package com.gridmobi.xrtb.domain.dsp.sys;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

public class JSysUserMapping implements Serializable {
    private JSysUser user;
    private boolean isSuper;
    private String sessionId;
    private Integer userType;
    private Integer publisherid;
    private List<JSysRole> roles;

    private List<String> accessAffList;
    private List<String> accessPubList;

    public List<JSysRole> getRoles() {
        return roles;
    }

    public Integer getPublisherid() {
        return publisherid;
    }

    public void setPublisherid(Integer publisherid) {
        this.publisherid = publisherid;
    }

    public void setRoles(List<JSysRole> roles) {

        this.roles = roles;
        accessAffList = Lists.newArrayList("-100");
        accessPubList =  Lists.newArrayList("-100");
        Set<String> set = Sets.newHashSet();
        Set<String> pubset = Sets.newHashSet();
        AtomicBoolean accessAll = new AtomicBoolean(false);

        roles.forEach(role -> {
            if (role.getAffauth() != null) {
                for (String affid : role.getAffauth().split(",")) {
                    if(StringUtils.isNotBlank(affid)){

                        set.add(affid.trim());
                    }
                }
            }


            if (role.getPubauth() != null) {
                for (String pubid : role.getPubauth().split(",")) {
                    if(StringUtils.isNotBlank(pubid)){
                        pubset.add(pubid.trim());
                    }
                }
            }

            if(1 ==role.getRoleid()|| -1 ==role.getRoleid() ){
                accessAll.set(true);
            }



        });
        if(accessAll.get()){
            accessAffList = Lists.newLinkedList();

        }else{

            accessAffList.addAll(set);

        }

    }

    public List<String> getAccessAffList() {
        return accessAffList;
    }

    public JSysUser getUser() {
        return user;
    }

    public void setUser(JSysUser user) {
        this.user = user;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    public boolean isSuper() {
        return isSuper;
    }

    public void setSuper(boolean aSuper) {
        isSuper = aSuper;
    }

    public void setAccessAffList(List<String> accessAffList) {
        this.accessAffList = accessAffList;
    }

    public List<String> getAccessPubList() {
        return accessPubList;
    }

    public void setAccessPubList(List<String> accessPubList) {
        this.accessPubList = accessPubList;
    }
}
